package com.videoaggre.zcl.ui.activity.wallpaper.enterdesk

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Environment
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.videoaggre.zcl.R
import com.videoaggre.zcl.api.ApiObserver
import com.videoaggre.zcl.bean.EnterdeskImage
import com.videoaggre.zcl.common.permissionRequest
import com.videoaggre.zcl.common.toastMessageLong
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.activity.launchUI
import com.videoaggre.zcl.ui.dialog.LoadingDialog
import com.videoaggre.zcl.ui.dialog.showAlertDialog
import com.videoaggre.zcl.utils.AlbumUtils
import com.yanzhenjie.permission.runtime.Permission
import com.yanzhenjie.sofia.Utils
import kotlinx.android.synthetic.main.enterdesk_activity_image_detail.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class EnterdeskImageDetailActivity : BaseActivity(R.layout.enterdesk_activity_image_detail) {
    private val mEnterdeskImage: EnterdeskImage by InjectBundle()
    private var mLoadingDialog: LoadingDialog? = null
    private var mUrl: String? = null
    override fun initView() {
        Utils.invasionStatusBar(this)
        Utils.invasionNavigationBar(this)
        bindViewClick(ivClose, buttonSaveAlbum)
        mLoadingDialog = LoadingDialog(this)
    }

    override fun initData() {
        mUrl = mEnterdeskImage.url.replace("_360_360", "")
        mLoadingDialog?.show()
        Glide.with(this)
            .asBitmap()
            .load(mUrl)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    ivImage.setImageBitmap(resource)
                    mLoadingDialog?.dismiss()
                }

                override fun onLoadCleared(placeholder: Drawable?) {}

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    mLoadingDialog?.dismiss()
                }
            })
    }

    override fun onClick(v: View?) {
        when (v) {
            ivClose -> finish()
            buttonSaveAlbum -> {
                if (mUrl == null) return
                showAlertDialog {
                    message(message = "确定保存到相册吗?")
                    negativeButton { }
                    positiveButton {
                        if (Build.VERSION.SDK_INT < 29 || Environment.isExternalStorageLegacy()) {
                            permissionRequest {
                                permission(Permission.WRITE_EXTERNAL_STORAGE)
                                onGranted { saveBitmap() }
                                onDenied { "未授权无法保存".toastMessageLong() }
                            }
                        } else {
                            saveBitmap()
                        }
                    }
                }
            }
        }
    }

    private fun saveBitmap() {
        buttonSaveAlbum.enable(false)
        launchUI({
            AlbumUtils.saveBitmap(this@EnterdeskImageDetailActivity, load(), "${System.currentTimeMillis()}.png")
        }, ApiObserver(complete = { buttonSaveAlbum.enable(true) }) {
            "保存成功,请去相册查看".toastMessageLong()
        })
    }

    private suspend fun load() = withContext(Dispatchers.IO) {
        Glide.with(this@EnterdeskImageDetailActivity)
            .asFile()
            .load(mUrl)
            .submit()
            .get()
    }
}